package com.zkyc.framework.website.util;

import com.alibaba.fastjson.JSONObject;
import com.zkyc.framework.common.pojo.User;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.website.client.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author bing.bai
 * @create 2022/5/26
 */
@Component
public class TokenUserUtil {

    @Autowired
    private UserClient userClient;

    public static String getCurrentUserId() {
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String userId = sra.getRequest().getHeader("userId");
        return userId;
    }


    public  User getCurrentUser() {
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String userId = sra.getRequest().getHeader("userId");
        if (userId != null) {
            ResultVO<?> userById = userClient.getUserById(userId);
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(userById));
            return JSONObject.parseObject(jsonObject.getString("data"), User.class);
        }
        return null;
    }
}